<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  
  	<xsl:template match="HeaderAndClass">

/* NOTE THESE LINES CREATE A DATABASE CALLED <xsl:value-of select="HeaderInfo/ProjectName"/>
 * IF THIS IS NOT THE NAME OF THE DATABASE YOU WANT TO CREATE MODIFY THESE AND YOUR 
 * CONNECTION STRING WITHIN THE ACTIVERECORD.XML FILE
 */
CREATE DATABASE [<xsl:value-of select="HeaderInfo/ProjectName"/>]
GO
USE [<xsl:value-of select="HeaderInfo/ProjectName"/>]
GO
	
BEGIN TRANSACTION
     
      <xsl:for-each select="Classes/Class">

PRINT 'Creating Table [<xsl:value-of select="Name"/>]'

CREATE TABLE [<xsl:value-of select="Name"/>] (
	[<xsl:value-of select="Name"/>ID] [uniqueidentifier] NOT NULL,
	[Modified] [datetime] NOT NULL,
		<xsl:for-each select="ClassAttribs/ClassAttributes">
			<xsl:if test="CType='single'">
	[<xsl:value-of select="Name"/>] <xsl:value-of select="SqlType"/> NULL,
			</xsl:if>
		</xsl:for-each>
	CONSTRAINT [PK_<xsl:value-of select="Name"/>] PRIMARY KEY CLUSTERED ( [<xsl:value-of select="Name"/>ID] ) ON [PRIMARY]
) ON [PRIMARY]
GO


        
      </xsl:for-each>
      
      
      <xsl:for-each select="Classes/Class">
        <xsl:variable name="Name" select="Name"/>
        
        

		<xsl:for-each select="ClassAttribs/ClassAttributes">
			<xsl:if test="CType='array'">
			


PRINT 'Creating Table [<xsl:value-of select="$Name"/>_<xsl:value-of select="Name"/>]'
			
CREATE TABLE [<xsl:value-of select="$Name"/>_<xsl:value-of select="Name"/>] (
	[<xsl:value-of select="$Name"/>ID] [uniqueidentifier] NOT NULL,
	[<xsl:value-of select="Type"/><xsl:if test="$Name=Type">Item</xsl:if>ID] [uniqueidentifier] NOT NULL,
	CONSTRAINT [PK_<xsl:value-of select="$Name"/>_<xsl:value-of select="Name"/>] PRIMARY KEY CLUSTERED (
		[<xsl:value-of select="$Name"/>ID],	
		[<xsl:value-of select="Type"/><xsl:if test="$Name=Type">Item</xsl:if>ID]
	) ON [PRIMARY],
	
	CONSTRAINT [FK_<xsl:value-of select="$Name"/>_<xsl:value-of select="Type"/>] FOREIGN KEY([<xsl:value-of select="$Name"/>ID])
	REFERENCES [<xsl:value-of select="Type"/>] ([<xsl:value-of select="Type"/>ID]),

	CONSTRAINT [FK_<xsl:value-of select="Type"/><xsl:if test="$Name=Type">Item</xsl:if>_<xsl:value-of select="$Name"/>] FOREIGN KEY([<xsl:value-of select="Type"/><xsl:if test="$Name=Type">Item</xsl:if>ID])
	REFERENCES [<xsl:value-of select="$Name"/>] ([<xsl:value-of select="$Name"/>ID])
	 
) ON [PRIMARY]	
GO
			</xsl:if>
		</xsl:for-each>  
        
      </xsl:for-each>
      
      
      
      COMMIT
      
    </xsl:template>
  

</xsl:stylesheet>